<?php

//搜索模块
class Search_model extends Model {

	function Search_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("site_model", "sm");
		$this->load->model("accounts_model", "am");
		$this->load->model("comment_model");
	}
	
	//计算总数
	function counts($keyword, $type)
	{
		switch ($type){
			case "site":
				$query = $this->db->query("select s.*,i.siteid,i.logo, replace(s.name,'$keyword', '<em class=word_s>$keyword</em>') as title, replace(i.description,'$keyword', '<em class=word_s>$keyword</em>') as description from rn_site as s left join rn_site_info as i on i.siteid=s.id where s.complete=1 and s.status=1 and (LOCATE('$keyword', s.name)>0 || LOCATE('$keyword', i.description)>0)");
			break;
			case "user":
				$query = $this->db->query("select *, replace(username,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_accounts where LOCATE('$keyword', username)>0");
			break;
			default:
				$query = $this->db->query("select *, replace(title,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_module_forum where LOCATE('$keyword', title)>0 order by id desc");
		}
		return $query->num_rows();
	}
	
	//话题搜索
	function topic($keyword, $limit = 2, $offset = 0)
	{
		$result = array();
		$query = $this->db->query("select *, replace(title,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_module_forum where LOCATE('$keyword', title)>0 order by id desc limit $offset,$limit");
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result  as $k => $row) {
				//小站信息
				$site = $this->sm->get_site($row["siteid"]);
				$result[$k]['domain'] = $site["domain"];
				//用户信息
				$userid = $row["userid"];
				$userinfo = $this->am->get($userid);
				$result[$k]["userinfo"] = $userinfo;
				//评论总数
				$commentcount = $this->comment_model->count_forum_lists($site["id"], $row["id"]);
				$result[$k]["commentcount"] = $commentcount;
			}
		}
		return $result;
	}
	
	//用户搜索
	function user($keyword, $limit = 2, $offset = 0)
	{
		$result = array();
		$query = $this->db->query("select *, replace(username,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_accounts where LOCATE('$keyword', username)>0 order by id desc limit $offset,$limit");
		if ($query->num_rows()>0) {
			$result = $query->result_array();
		}
		return $result;
	}
	
	//热站搜索
	function site($keyword, $limit = 2, $offset = 0)
	{
		$result = array();
		$query = $this->db->query("select s.*,i.siteid,i.logo, replace(s.name,'$keyword', '<em class=word_s>$keyword</em>') as title, replace(i.description,'$keyword', '<em class=word_s>$keyword</em>') as description from rn_site as s left join rn_site_info as i on i.siteid=s.id where s.complete=1 and s.status=1 and (LOCATE('$keyword', s.name)>0 || LOCATE('$keyword', i.description)>0) order by id desc limit $offset,$limit");
		if ($query->num_rows()>0) {
			$result = $query->result_array();
		}
		return $result;
	}
	
}

/* End of file */